import { got } from "@/got";

export function getUserOpenid(js_code: string) {
  return got({
    url: "/open-api/user-openid",
    method: "POST",
    data: { js_code }
  });
}

export function checkUserLogin() {
  return got({
    url: "/auth-api/current"
  });
}

export function loginByOpenid(openid: string) {
  return got({
    url: "/open-api/login/openid",
    method: "POST",
    data: { openid }
  });
}

export function getUserPhoneInfoApi(code: string) {
  return got({
    url: "/open-api/user-phone",
    method: "POST",
    data: { code }
  });
}

export function regOrLoginApi(login_code: string, phone_code: string) {
  return got({
    url: "/open-api/user-check",
    method: "POST",
    data: { login_code, phone_code }
  });
}

export function syncWxUserInfo(target: string, iv: string, login_code: string) {
  return got({
    url: "/auth-api/sync-wx-info",
    method: "POST",
    data: { target, iv, login_code }
  });
}

export function updateUserInfo(options: { user_avatar?: string; user_nickname?: string }) {
  if (options.user_avatar) {
    return got.upload({
      url: "/auth-api/wx/user/update",
      filePath: options.user_avatar,
      formData: {
        user_nickname: options.user_nickname
      }
    });
  } else {
    return got({
      url: "/auth-api/wx/user/update/nickname",
      method: "POST",
      data: {
        user_nickname: options.user_nickname
      }
    });
  }
}
